package cn.how2go.api.task;

import cn.how2go.api.common.SocketChannels;
import cn.how2go.api.entity.Cone;
import cn.how2go.api.entity.Person;
import cn.how2go.api.entity.UserInfo;
import cn.how2go.api.entity.Warning;
import cn.how2go.api.service.ConeService;
import cn.how2go.api.service.UserInfoService;
import cn.how2go.api.service.WarningService;
import cn.how2go.api.util.DateUtil;
import cn.how2go.api.util.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2019/4/12.
 */
@Configuration
public class ScheduledTask {

    @Autowired
    ConeService coneService;

    @Autowired
    WarningService warningService;

    @Autowired
    UserInfoService userInfoService;

    @Value("${scheduled.checkworking.disworktime}")
    private int disworktime;

    @Scheduled(cron = "${scheduled.checkworking.cron}")
    public void checkWorking(){
        //System.out.println("开始工作");
        //查询出所有在线筒锥
        EntityWrapper<Cone> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("Working",1);
        List<Cone> list = coneService.selectList(entityWrapper);
        for(Cone bean : list){
            //最后更新时间
            Date lastworktime = bean.getLastTime();
            //当前时间
            Date now  = new Date();
            //更新时间和当前时间差距多少秒
            //如果大于3*60（即3分钟）就是说3分钟没有数据更新锥桶就是离线状态
            if(DateUtil.timepk(lastworktime,now)>disworktime*60){
                Cone param = new Cone();
                //设置为离线状态
                param.setWorking(2);
                param.setLastTime(new Date());
                EntityWrapper<Cone> updateWrapper = new EntityWrapper<>();
                updateWrapper.eq("id",bean.getId());
                coneService.update(param,updateWrapper);
            }
        }
        //System.out.println("结束工作");
    }

    @Scheduled(cron = "*/10 * * * * ?")
    public void sendWarnning(){
        EntityWrapper<Warning> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("Alarm",0);
        entityWrapper.ne("State",1);
        entityWrapper.ne("State",2);
        List<Warning> list = warningService.selectList(entityWrapper);

        if(list.size()>0) {
            String sendmsg = "";
            String ids = "";
            for(Warning bean:list){
                Person u = userInfoService.selectUserInfoOnce(bean.getOwner());
                sendmsg+=ScheduledTask.getTypeName(bean.getType())+";"+u.getName()+";"+DateUtil.format(bean.getRecordTime(),"yyyy-MM-dd HH:mm:ss")+",";
                ids += bean.getId()+",";
            }
            SocketChannels.channels.writeAndFlush(new TextWebSocketFrame(sendmsg));
            Warning alarm  = new Warning();
            //设置为已告警
            alarm.setAlarm(1);
            EntityWrapper<Warning> entityWrapper1 = new EntityWrapper<>();
            entityWrapper1.in("id",ids+"0");
            warningService.update(alarm,entityWrapper1);
        }


    }

    public static String getTypeName(Integer warnning){
        String type = "";
        switch(warnning) {
            case 1:
                type = "电子围栏禁入告警";
                break;
            case 2:
                type = "电子围栏禁出告警";
                break;
            case 3:
                type = "体征异常告警";
                break;
            case 4:
                type = "工器佩戴";
                break;
            case 5:
                type = "高挂低用";
                break;
            case 6:
                type = "SOS报警";
                break;
            case 7:
                type = "跌落";
                break;
            case 8:
                type = "近电提醒";
                break;
            case 9:
                type = "离开近电";
                break;
            case 10:
                type = "安全帽未佩戴";
                break;
            default:
                type="";
        }
        return type;
    }

}
